package com.fahai.common;

import com.github.pagehelper.Page;

import java.util.List;

/**
 * Created by pc on 2016/8/12.
 * 返回前台分页的结果集对象
 */
public class PageVo {

    /** 当前页 **/
    private Integer pageNum = 1;
    /** 页大小 **/
    private Integer pageSize = 10;
    /** 总行数 **/
    private Long total;
    /** 结果集 **/
    private List<?> result;

    private int pages;

    public PageVo() {}

    public PageVo(Page<?> page, List<?> result) {
        this.setPageNum(page.getPageNum());
        this.setPageSize(page.getPageSize());
        this.setTotal(page.getTotal());
        this.setResult(result);
    }

    public List<?> getResult() {
        return result;
    }

    public void setResult(List<?> result) {
        this.result = result;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
        if(total == -1L) {
            this.pages = 1;
        } else {
            if(this.pageSize > 0) {
                this.pages = (int)(total / (long)this.pageSize + (long)(total % (long)this.pageSize == 0L?0:1));
            } else {
                this.pages = 0;
            }
        }
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    /**
     * 创建Page
     * @param page
     * @param result
     * @return
     */
    public static PageVo createPageVo(Page<?> page, List<?> result) {
        return new PageVo(page,result);
    }

}
